package com.mentalhealthplatform.teacherV2.controller;

import com.mentalhealthplatform.appModels.Test;
import com.mentalhealthplatform.teacherV2.service.TestServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/tests")
@CrossOrigin
public class TestControllerV2 {

    @Autowired
    private TestServiceV2 testServiceV2;

    // 获取所有测试的端点
    @GetMapping
    public List<Test> getAllTests() {
        return testServiceV2.findAll();
    }

    // 根据ID获取特定测试的端点
    @GetMapping("/{id}")
    public ResponseEntity<Test> getTestById(@PathVariable Integer id) {
        Optional<Test> test = testServiceV2.findById(id);
        return test.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }

    // 新增测试的端点
    @PostMapping
    public Test createTest(@RequestBody Test test) {
        return testServiceV2.save(test);
    }

    // 更新测试的端点
    @PutMapping("/{id}")
    public ResponseEntity<Test> updateTest(@PathVariable Integer id, @RequestBody Test testDetails) {
        Optional<Test> test = testServiceV2.findById(id);
        if (test.isPresent()) {
            Test updatedTest = test.get();
            updatedTest.setTopic(testDetails.getTopic());
            updatedTest.setAlways(testDetails.getAlways());
            updatedTest.setOften(testDetails.getOften());
            updatedTest.setSometimes(testDetails.getSometimes());
            updatedTest.setHardly(testDetails.getHardly());
            updatedTest.setNone(testDetails.getNone());
            updatedTest.setTypes(testDetails.getTypes());
            return ResponseEntity.ok(testServiceV2.save(updatedTest));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除测试的端点
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTest(@PathVariable Integer id) {
        testServiceV2.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    // 根据主题搜索测试的端点
    @GetMapping("/search")
    public ResponseEntity<List<Test>> searchTests(@RequestParam String keyword) {
        List<Test> tests = testServiceV2.searchByTopic(keyword);
        return ResponseEntity.ok(tests);
    }

    // 根据类型搜索测试的端点
    @GetMapping("/types")
    public ResponseEntity<List<Test>> getTestsByTypes(@RequestParam String types) {
        try {
            Set<Integer> typeSet = Arrays.stream(types.split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toSet());
            List<Test> tests = testServiceV2.findByTypes(typeSet);
            return ResponseEntity.ok(tests);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /*
    // 根据关键词搜索测试的端点
    @GetMapping("/global-search")
    public ResponseEntity<List<Test>> globalSearch(@RequestParam String query) {
        List<Test> tests = testServiceV2.searchGlobally(query);
        return ResponseEntity.ok(tests);
    }*/
}